package org.ragdollcat.first.manager.disruptor;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

@Configuration
public class DisruptorConfig {

    @Resource
    @Lazy
    private EditPictureWorkHandler editPictureWorkHandler;

    /**
     * 自定义Disruptor
     * @return
     */
    @Bean("disruptor")
    public Disruptor<EditPictureDisruptorEvent> disruptor() {
        int bufferSize = 1024 * 256;
        Disruptor<EditPictureDisruptorEvent> disruptor = new Disruptor<>(
                EditPictureDisruptorEvent::new,
                bufferSize,
                ThreadFactoryBuilder.create().setNamePrefix("disruptor_").build()
        );
        //设置消费者
        disruptor.handleEventsWithWorkerPool(editPictureWorkHandler);
        disruptor.start();
        return disruptor;
    }

}
